package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.CourierStationDao;
import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.dao.impl.CommunityDaoImpl;
import com.hs.panghu.dao.impl.CourierStationDaoImpl;
import com.hs.panghu.dao.impl.GoodsDaoImpl;
import com.hs.panghu.entity.Community;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.annotation.ElementType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierStation")
public class CourierStationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CourierStationDao courierStationDao=new CourierStationDaoImpl();
        if ("querySearchCourierStationList".equals(action)){
            String courierStationName = req.getParameter("courierStationName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationStatus = req.getParameter("courierStationStatus");
            String communityName = req.getParameter("communityName");
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");

            Map<String,String>map=new HashMap<>();
            map.put("courierStationName",courierStationName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("courierStationStatus",courierStationStatus);
            map.put("communityName",communityName);
            map.put("courierStationCommunityName",courierStationCommunityName);

            List<CourierStation> list = courierStationDao.selectCourierStations(map);

            Map<String,String>map1=new HashMap<>();
            String pageNum1 = req.getParameter("pageNum");
            String pageSize1 = req.getParameter("pageSize");
            map1.put("pageNum",pageNum1);
            map1.put("pageSize",pageSize1);
            CommunityDao communityDao=new CommunityDaoImpl();
            List<Community> list1 = communityDao.selectCommunitys(map1);

            int count = courierStationDao.selectCourierStationCount(map);

            Map map3=new HashMap();
            map3.put("list",list);
            map3.put("total",count);
            map3.put("list1",list1);

            Result success = Result.success(map3);

            resp.getWriter().write(JSON.toJSONString(success));

        }else if ("modifyCourierStation".equals(action)){
            String courierStationId = req.getParameter("courierStationId");
            int row = courierStationDao.modifyCourierStation(Integer.parseInt(courierStationId));
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }else  if ("addCourierStation".equals(action)){
            String courierStationName = req.getParameter("courierStationName");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationSort = req.getParameter("courierStationSort");
            if ("".equals(courierStationSort)||courierStationSort==null){
                courierStationSort="0";
            }
            String courierStationCommunityId = req.getParameter("courierStationCommunityId");
            if ("".equals(courierStationCommunityId)||courierStationCommunityId==null){
                courierStationCommunityId="0";
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationOrderCount = req.getParameter("courierStationOrderCount");
            if ("".equals(courierStationOrderCount)||courierStationOrderCount==null){
                courierStationOrderCount="0";
            }

            CourierStation courierStation=new CourierStation(
                    null,
                    courierStationName,
                    courierStationImage,
                    courierStationProvince,
                    courierStationCity,
                    courierStationDistrict,
                    courierStationAddress,
                    Integer.parseInt(courierStationSort),
                    Integer.parseInt(courierStationCommunityId),
                    courierStationCommunityName,
                    Integer.parseInt(courierStationOrderCount)
            );
            int row = courierStationDao.insertCourierStation(courierStation);
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }else if ("modifyCourierStation1".equals(action)){
            String courierStationName = req.getParameter("courierStationName");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationId = req.getParameter("courierStationId");

            String courierStationSort = req.getParameter("courierStationSort");
            if ("".equals(courierStationSort)||courierStationSort==null){
                courierStationSort="0";
            }
            String courierStationCommunityId = req.getParameter("courierStationCommunityId");
            if ("".equals(courierStationCommunityId)||courierStationCommunityId==null){
                courierStationCommunityId="0";
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationOrderCount = req.getParameter("courierStationOrderCount");
            if ("".equals(courierStationOrderCount)||courierStationOrderCount==null){
                courierStationOrderCount="0";
            }

            CourierStation courierStation=new CourierStation(
                    Integer.parseInt(courierStationId),
                    courierStationName,
                    courierStationImage,
                    courierStationProvince,
                    courierStationCity,
                    courierStationDistrict,
                    courierStationAddress,
                    Integer.parseInt(courierStationSort),
                    Integer.parseInt(courierStationCommunityId),
                    courierStationCommunityName,
                    Integer.parseInt(courierStationOrderCount)
            );
            int row = courierStationDao.updateCourierStation(courierStation);
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
